#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* ch1 = head;
    struct ListNode* ch2 = NULL;
    while (ch1)
    {
        ch2 = ch1->next;
        if (ch1->val == val)
        {
            if (ch1 == head)
            {
                head = ch2;
                free(ch1);
                ch1 = ch2;
                ch2 = ch2->next;
            }
        }
        else if (ch1->next->val == val)
        {
            ch2 = ch2->next;
            free(ch1->next);
            ch1->next = ch2;
        }
        else
        {
            ch1 = ch1->next;
            ch2 = ch2->next;
        }

    }
    return head;
}